<script setup>
import {ref} from "vue"
import {articleDetailGetService, articleAddService, articleEditService} from "@/api/article"
import {baseURL} from '@/utils/request'
import {imageUrlToFile} from '@/utils/format'
import {Plus} from "@element-plus/icons-vue"
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'

const data = {
  title: '',
  cate_id: '',
  content: '',
  state: '',
  cover_img: null
}

const formModel = ref({...data})


const drawer = ref(false)
const editorContent = ref()

const open = async (row) => {
  drawer.value = true
  if (row.id) {
    // 获取详情
    const res = await articleDetailGetService(row.id)
    formModel.value = res.data
    // 图片回显特殊处理
    imageUrl.value = baseURL + formModel.value.cover_img
    // 将详情接口返回的图片URL转换成File
    formModel.value.cover_img = await imageUrlToFile(imageUrl.value, formModel.value.cover_img)
  } else {
    formModel.value = {...data}
    // 清空图片和富文本框
    imageUrl.value = null
    if (editorContent.value) {
      editorContent.value.setHTML(null)
    }
  }
}

// 暴露给父组件 打开dialog会话框
defineExpose({
  open
})

// 图片预览处理
const imageUrl = ref()
// 从本地选了图片会回调这个钩子方法 给imgUrl设置路径
const preview = (uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  formModel.value.cover_img = uploadFile.raw
}

// 发布文章
const emit = defineEmits(['success'])
const publishArticle = async state => {
  formModel.value.state = state
  const fd = new FormData()
  for(let key in formModel.value) {
    fd.append(key, formModel.value[key])
  }
  if (formModel.value.id) {
    await articleEditService(fd)
    // 通知父组件更新列表
    emit('success', 'edit')
  } else {
    await articleAddService(fd)
    // 通知父组件更新列表
    emit('success', 'add')
  }
  ElMessage.success('发布成功')
  // 关闭窗口
  drawer.value = false
}

</script>

<template>
  <el-drawer
    v-model="drawer"
    :title="formModel.id ? '编辑文章' : '添加文章'"
    direction="rtl"
    size="50%"
  >
    <!-- 发表文章表单 -->
    <el-form :model="formModel" label-width="100px">
      <el-form-item label="文章标题">
        <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="文章分类">
        <channel-select
          v-model="formModel.cate_id"
          width="100%"
        ></channel-select>
      </el-form-item>
      <el-form-item label="文章封面">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="preview"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" alt=""/>
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容">
        <div class="editor">
          <QuillEditor ref="editorContent" theme="snow" v-model:content="formModel.content" contentType="html"/>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="publishArticle('已发布')">发布</el-button>
        <el-button type="info" @click="publishArticle('草稿')">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
